<template>
  <div class="login">
    <van-nav-bar title="会员登录" left-arrow @click-left="$router.go(-1)"/>
    <div class="title">
      <h3>手机号登录</h3>
      <p>未注册的手机号登录后自动注册</p>
    </div>

    <div class="form">
      <div class="form-item">
        <input class="inp" type="text" placeholder="请输入手机号" v-model="mobile" maxlength="11">
      </div>
      <div class="form-item">
        <input class="inp" type="text" placeholder="请输入图形验证码" v-model="picCode" maxlength="4">
        <img :src="picUrl" alt="" @click="getPicCode" v-if="picUrl">
      </div>
      <div class="form-item">
        <input class="inp" type="text" placeholder="请输入短信验证码" v-model.number="smsCode" maxlength="6">
        <button @click="getCode">
          {{ second === totalSecond ? '获取验证码' : second + '秒后重新发送' }}
        </button>
      </div>
    </div>
    <div @click="login" class="login-btn">登录</div>
  </div>
</template>

<script>
import { getPicCode, getMsgCode, codeLogin } from '@/api/login'

export default {
  name: 'LoginPage',
  data () {
    return {
      mobile: '',
      picCode: '',
      smsCode: '',
      picUrl: '',
      picKey: '',
      second: 60,
      totalSecond: 60,
      timer: null
    }
  },
  created () {
    this.getPicCode()
  },
  methods: {
    async login() {
      if (!this.validFn()) {
        return
      }
      if (!/^\d{6}/.test(this.smsCode)) {
        this.$toast('请输入正确的手机验证码')
        return
      }
      const res = await codeLogin(this.mobile, this.smsCode)
      this.$store.commit('user/setUserInfo', res.data)
      this.$toast('登录成功')

      // 进行判断，看地址栏有无回跳地址
      // 1. 如果有   => 说明是其他页面，拦截到登录来的，需要回跳
      // 2. 如果没有 => 正常去首页
      const url = this.$route.query.backUrl || '/'
      this.$router.replace(url)
    },
    // 获取短信验证码
    async getCode () {
      if (!this.validFn()) {
        return
      }
      if (!this.timer && this.second === this.totalSecond) {
        await getMsgCode(this.picKey, this.picKey, this.mobile)

        this.$toast('短信发送成功，注意查收')

        // 开启倒计时
        this.timer = setInterval(() => {
          this.second--
          if (this.second <= 0) {
            clearInterval(this.timer)
            this.timer = null
            this.second = this.totalSecond // 归位
          }
        }, 1000)
      }
    },
    // 获取图形验证码
    async getPicCode () {
      const { data: { base64, key } } = await getPicCode()
      this.picUrl = base64
      this.picKey = key
    },
    // 校验 手机号 和 图形验证码 是否合法
    // 通过校验，返回true
    // 不通过校验，返回false
    validFn() {
      if (!/^1[3-9]\d{9}$/.test(this.mobile)) {
        this.$toast('请输入正确的手机号码')
        return false
      }
      if (!/^\w{4}$/.test(this.picCode)) {
        this.$toast('请输入正确的图形验证码')
        return false
      }
      return true
    }
  },
  destroyed () {
    clearInterval(this.timer)
  }
}
</script>

<style lang="less" scoped>
.title {
  padding: 20px;
  margin-top: 30px;
  h3 {
    font-size: 24px;
    font-weight: 400;
  }
  p {
    color: #999;
    font-size: 14px;
    margin-top: 10px;
  }
}
.form {
  padding: 20px;
  .form-item {
    display: flex;
    justify-content: space-between;
    margin-bottom: 8px;
    border-bottom: 1px solid #dcdcdc;
    align-items: center;
    .inp {
      flex: 1;
      height: 45px;
      border: none;
      outline: none;
      font-size: 14px;
    }
    img {
      width: 94px;
      height: 31px;
    }
    button {
      border: none;
      background-color: transparent;
      font-size: 14px;
      color: #cea26a;
      padding-right: 10px;
    }
  }
}
.login-btn {
  display: flex;
  height: 42px;
  margin: 20px;
  background: linear-gradient(90deg, #ecb53c, #ff9211);
  color: white;
  border-radius: 40px;
  text-align: center;
  line-height: 42px;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
